package org.jeecg.modules.recruit.withdraw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.api.dto.req.user.WithdrawApplyDto;
import org.jeecg.modules.recruit.appuser.entity.MmAppUser;
import org.jeecg.modules.recruit.appuser.service.IMmAppUserService;
import org.jeecg.modules.recruit.remittance.entity.MmAppRemittance;
import org.jeecg.modules.recruit.remittance.service.IMmAppRemittanceService;
import org.jeecg.modules.recruit.withdraw.entity.MmAppUserWithdraw;
import org.jeecg.modules.recruit.withdraw.mapper.MmAppUserWithdrawMapper;
import org.jeecg.modules.recruit.withdraw.service.IMmAppUserWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 用户提现申请
 * @Author: jeecg-boot
 * @Date: 2020-08-21
 * @Version: V1.0
 */
@Service
public class MmAppUserWithdrawServiceImpl extends ServiceImpl<MmAppUserWithdrawMapper, MmAppUserWithdraw> implements IMmAppUserWithdrawService {

    @Autowired
    private IMmAppUserService mmAppUserService;

    @Autowired
    private IMmAppRemittanceService mmAppRemittanceService;

    @Override
    @Transactional(rollbackFor = {IllegalArgumentException.class, Exception.class})
    public boolean withdrawApply(WithdrawApplyDto apply) {
        LambdaQueryWrapper<MmAppUser> queryWrapper = Wrappers.<MmAppUser>lambdaQuery().eq(MmAppUser::getId, apply.getUserId());
        MmAppUser one = mmAppUserService.getOne(queryWrapper);
        if (!Integer.valueOf(1).equals(one.getIsAuth())) {
            throw new IllegalArgumentException("请先实名认证");
        }
        int amount = (int) (apply.getAmount() * 100);
        MmAppUserWithdraw withdraw = new MmAppUserWithdraw();
        withdraw.setAmount(amount);
        withdraw.setUserId(apply.getUserId());
        this.getBaseMapper().insert(withdraw);
        //更新用可提现余额
        if (!mmAppUserService.minusAmount(apply.getUserId(), amount)) {
            throw new IllegalArgumentException("可提现余额不足！");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {IllegalArgumentException.class, Exception.class})
    public boolean withdrawApplyModify(MmAppUserWithdraw withdraw) {
        //1. 修改提现状态
        updateById(withdraw);
        //2. 如果为审核通过 则创建打款单
        if (withdraw.getStatus().equals(2)) {
            MmAppRemittance mmAppRemittance = new MmAppRemittance();
            mmAppRemittance.setApplyId(withdraw.getId());
            mmAppRemittance.setUserId(withdraw.getUserId());
            mmAppRemittance.setOperatorManager(withdraw.getOperatorManager());
            mmAppRemittance.setOperatorManagerName(withdraw.getOperatorManagerName());
            mmAppRemittance.setRemitAmount(withdraw.getAmount());
            mmAppRemittanceService.createRemittance(mmAppRemittance);
        }
        return true;
    }
}
